//
//  findPasswordViewController.m
//  忘不了
//
//  Created by rimi on 16/7/10.
//  Copyright © 2016年 handuan. All rights reserved.
//

#import "findPasswordViewController.h"
#import "SDAutoLayout.h"
#import "BaseRequest.h"
#import "findPasswordSucceedViewController.h"
#import "CheckWeb.h"
@interface findPasswordViewController ()
@property (nonatomic,strong)UILabel *tipLabel;
@property (nonatomic,strong)UITextField *emailTextField;
@property (nonatomic,strong)UIButton *postEmailButton;
@property (nonatomic,strong)UILabel *warningLabel;

@end

@implementation findPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60)
                                                         forBarMetrics:UIBarMetricsDefault];//隐藏导航栏返回按钮自带的文字
    self.title = @"找回密码";
    //设置导航栏标题颜色、字体大小等
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20],NSForegroundColorAttributeName:[UIColor brownColor]}];
    
    UIImageView *backGroudView = [[UIImageView alloc]initWithFrame:self.view.frame];
    backGroudView.image = [UIImage imageNamed:@"s.jpg"];
    [self.view addSubview:backGroudView];
    
    [self initInterface];
    
}

- (void)initInterface {
    _tipLabel = [[UILabel alloc]init];
    _tipLabel.textAlignment = NSTextAlignmentCenter;
    _tipLabel.text = @"请输入注册时所填写的邮箱";
    _tipLabel.textColor = [UIColor brownColor];
    _tipLabel.font = [UIFont systemFontOfSize:20];
    [self.view addSubview:_tipLabel];
    _tipLabel.sd_layout
    .leftSpaceToView(self.view,30)
    .topSpaceToView(self.view,150)
    .rightSpaceToView(self.view,30)
    .heightIs(self.view.frame.size.width*0.1);
    
    _emailTextField = [[UITextField alloc]init];
    _emailTextField.backgroundColor = [UIColor whiteColor];
    _emailTextField.textColor = [UIColor blackColor];
    _emailTextField.font = [UIFont systemFontOfSize:20];
    _emailTextField.layer.cornerRadius = 10;
    _emailTextField.layer.masksToBounds = YES;
    _emailTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self.view addSubview:_emailTextField];
    _emailTextField.sd_layout
    .leftEqualToView(_tipLabel)
    .topSpaceToView(_tipLabel,50)
    .rightEqualToView(_tipLabel)
    .heightIs(self.view.frame.size.width*0.1);
    
    _warningLabel = [[UILabel alloc]init];//邮箱错误提示
    _warningLabel.alpha = 0;
    _warningLabel.text = @"邮箱输入错误";
    _warningLabel.textAlignment = NSTextAlignmentCenter;
    _warningLabel.textColor = [UIColor redColor];
    _warningLabel.font = [UIFont systemFontOfSize:18];
    [self.view addSubview:_warningLabel];
    _warningLabel.sd_layout
    .leftEqualToView(_emailTextField)
    .topSpaceToView(_emailTextField,30)
    .rightEqualToView(_emailTextField)
    .heightRatioToView(_emailTextField,1);
    
    _postEmailButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [_postEmailButton setTitle:@"找回密码" forState:UIControlStateNormal];
    [_postEmailButton setTitleColor:[UIColor brownColor] forState:UIControlStateNormal];
    _postEmailButton.backgroundColor = [UIColor colorWithRed:0.902 green:0.8706 blue:0.8196 alpha:1.0];
    _postEmailButton.layer.cornerRadius = 10;
    _postEmailButton.layer.masksToBounds = YES;
    _postEmailButton.layer.borderWidth = 1;
    _postEmailButton.layer.borderColor = [UIColor colorWithRed:0.7569 green:0.7333 blue:0.5333 alpha:1.0].CGColor;
    [_postEmailButton addTarget:self action:@selector(postEmailButtonTaped) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_postEmailButton];
    _postEmailButton.sd_layout
    .leftSpaceToView(self.view,self.view.frame.size.width*0.4)
    .rightSpaceToView(self.view,self.view.frame.size.width*0.4)
    .topSpaceToView(_emailTextField,100)
    .heightIs(self.view.frame.size.width*0.1);
    
}

- (void)postEmailButtonTaped {
    if ([_emailTextField.text  isEqual: @""]) {
        UIAlertController *tips = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"邮箱不能为空" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
        [tips addAction:sure];
        [self presentViewController:tips animated:YES completion:nil];
    } else if ([self isValidateEmail:self.emailTextField.text] == NO) {
        _warningLabel.alpha = 1;
        [self performSelector:@selector(dismiss) withObject:nil afterDelay:2.0];
    } else {
        BOOL isWeb = [CheckWeb checkWang];
        if (isWeb == NO) {
            _warningLabel.alpha = 1;
            _warningLabel.text = @"请检查网络";
            [self performSelector:@selector(dismiss) withObject:nil afterDelay:2.0];
        }else{
            [BaseRequest startFindPasswordWithEmail:self.emailTextField.text completionhanlder:^(Response *response) {
                if (response.isSuccess) {
                    findPasswordSucceedViewController *succeedVC = [[findPasswordSucceedViewController alloc]init];
                    succeedVC.password = response.resultObject;
                    self.emailTextField.text = @"";
                    [self.navigationController pushViewController:succeedVC animated:YES];
                }else{
                    _warningLabel.alpha = 1;
                    _warningLabel.text = @"邮箱不存在";
                    [self performSelector:@selector(dismiss) withObject:nil afterDelay:2.0];
                }
            }];
            
        }
    }
}

//正则表达式验证邮箱
- (BOOL)isValidateEmail:(NSString *)email{
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:email];
}

//dismiss 警告提示框
- (void)dismiss
{
    _warningLabel.alpha = 0;
    [self dismissViewControllerAnimated:YES completion:nil];
}

//点空白回收键盘
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}


@end
